package com.bridge.common.base;

import android.content.Intent;
import android.net.Uri;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.bridge.common.R;
import com.bridge.common.databinding.ActivityWebviewBinding;
import com.bridge.common.utils.UltimateBarXUtils;

/**
 * @ClassName WebviewActivity
 * @Description TODO消息
 * @Author xushufeng
 * @CreateDate 2021/4/28 0028 21:04
 * @Version 1.0
 * @UpdateDate 2021/4/28 0028 21:04
 * @UpdateRemark 更新说明
 */
public class WebviewActivity extends BaseActivity {
    ActivityWebviewBinding binding;
    public String title;
    public String url;

    @Override
    protected int layoutId() {
        return R.layout.activity_webview;
    }

    @Override
    protected void initView() {
        binding = getBind();
        title=getIntent().getStringExtra("title");
        url=getIntent().getStringExtra("url");
        binding.titleView.setTitle(title);
        UltimateBarXUtils.setAppBar(this, true, true, R.color.white);
        initWebView();
    }

    @Override
    protected void setListenner() {

    }


    public void initWebView() {
        //启用JS脚本
        binding.webview.getSettings().setJavaScriptEnabled(true);
        //支持自动加载图片
        binding.webview.getSettings().setLoadsImagesAutomatically(true);
        //启用内置缩放装置
        binding.webview.getSettings().setSupportZoom(false);  //支持放大缩小
        binding.webview.getSettings().setBuiltInZoomControls(false); //显示缩放按钮
        //设置自适应屏幕，两者合用
        binding.webview.getSettings().setUseWideViewPort(true);  //将图片调整到适合webview的大小
        binding.webview.getSettings().setLoadWithOverviewMode(true); // 缩放至屏幕的大小
        binding.webview.getSettings().setAllowFileAccess(true); // 允许访问文件
//        if (NetworkUtil.isConnected(getApplicationContext())) {
//            webview.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);//根据cache-control决定是否从网络上取数据。
//        } else {
//            webview.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);//没网，则从本地获取，即离线加载
//        }
        binding.webview.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
        //覆盖WebView默认使用第三方或系统默认浏览器打开网页的行为，使网页用WebView打开
        binding.webview.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String uri) {
                // TODO Auto-generated method stub
                //返回值是true的时候控制去WebView打开，为false调用系统浏览器或第三方浏览器
                if (uri.startsWith("tel:")) {
                    Intent intent = new Intent(Intent.ACTION_VIEW,
                            Uri.parse(uri));
                    startActivity(intent);
                } else {
                    view.loadUrl(uri);
                }
                return true;
            }
        });

        binding.webview.loadUrl(url);
        //WebView 事件回调监听
        binding.webview.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {

                if (newProgress >=60) {
                    binding.llView.setVisibility(View.GONE);
                } else {
                    binding.llView.setVisibility(View.VISIBLE);
                }
                super.onProgressChanged(view, newProgress);
            }
        });
    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (binding.webview.canGoBack()) {
                binding.webview.goBack();
            } else {
                finish();
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public void onPause() {
        super.onPause();
        if(binding.webview!=null){
            binding.webview.onPause();
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        if(binding.webview!=null){
            binding.webview.onResume();
        }

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(binding.webview!=null){
            binding.webview.destroy();
        }

    }

    @Override
    public void onStop() {
        // TODO Auto-generated method stub
        super.onStop();
//        ll_view.setVisibility(View.GONE);
    }
}
